package com.hnas.common.util;

import java.math.BigDecimal;
import java.util.Date;
import java.util.UUID;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hnas.api.domain.ResultCode;
import com.hnas.api.dto.DTOResult;
import com.hnas.core.util.DateUtil;
import com.hnas.core.util.StringUtil;

public class CommonUtils {
	
	public static ObjectMapper objectMapper = new ObjectMapper();

	
	public static String  getUUID(){
		return UUID.randomUUID().toString().replace("-", "");
	}
	
	@SuppressWarnings("finally")
	public static Date getTranTime(String tranDate, String tranTime){
		Date res = null;
		try{
			res = DateUtil.stringToDate(tranDate+tranTime, "yyyyMMddHHmmss");
		} catch(Exception e){
			e.printStackTrace();
		} finally {
			return res;
		}
	}
	
	public static double halfUpScaleDouble(Double d,int fix){
		BigDecimal b= new BigDecimal(d);  
		double f1 = b.setScale(fix,BigDecimal.ROUND_HALF_UP).doubleValue(); 
		return f1;
	}
	
	public static String halfUpScaleDouble2(Double d,int fix){
		BigDecimal b= new BigDecimal(d);  
		double f1 = b.setScale(fix,BigDecimal.ROUND_HALF_UP).doubleValue(); 
		return Double.toString(f1);
	}
	
	/**
	 * 创建Controller返回结果
	 * @param dtoResult 返回结果对象
	 * @param code 返回码
	 * @param result 返回的数据JSON字符串
	 * @return
	 */
	public static String createResult(DTOResult dtoResult,String code,String result){
		String res = "";
		try{
			dtoResult.setResultCode(code);
			dtoResult.setResultMsg(ResultCode.getErrMsg(code));
			dtoResult.setResultData(result);
			res = objectMapper.writeValueAsString(dtoResult);
		}catch(Exception e1){
			e1.printStackTrace();
			dtoResult.setResultCode(ResultCode.FAIL);
			dtoResult.setResultMsg(ResultCode.getErrMsg(ResultCode.FAIL));
			dtoResult.setResultData(result);
			try{
				res = objectMapper.writeValueAsString(dtoResult);
			}catch(Exception e2){
				e2.printStackTrace();
			}
		}
		return res;
	}
	
	/**
	 * 获取6位流水号算法
	 * @param fileSize
	 * @return
	 */
	public static String getUploadFileSeq(int fileIndex,int seqLength){
		String str = "";
		String index = fileIndex+"";
		if(index.length() < seqLength){
			int num = seqLength-index.length();
			for(int i=0;i<num;i++){
				str+="0";
			}
		}
		return str+index;
	}
	
	
	public static String getChangeString(String mrchID){
		String mrchIDStr="";
		if(StringUtil.isNotEmpty(mrchID)){
			  String[] mrchIDs=mrchID.split(",");
			  for(String id:mrchIDs){
				  mrchIDStr=mrchIDStr+",'"+id+"'";
			  }
			  mrchIDStr=mrchIDStr.substring(1);
		}
		return mrchIDStr;
	}
}
